package ciyuan.wrap;

import ciyuan.QQLogin;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class BKMain extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        Wrapper.INSTANCE = new BKWrap(this);
        Bukkit.getPluginManager().registerEvents(this, this);
        QQLogin.onEnable();
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onLogin(PlayerJoinEvent event) {
        event.getPlayer().sendMessage(QQLogin.config.SuccessMsg);
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onJoin(AsyncPlayerPreLoginEvent event) {
        String address = event.getAddress().toString().replace("/", "");
        String kickMsg = QQLogin.onJoin(event.getName(), address);
        if (kickMsg == null && event.getLoginResult().equals(AsyncPlayerPreLoginEvent.Result.ALLOWED)) {
            event.allow();
        } else if (kickMsg != null) {
            event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, kickMsg);
        }
    }

}
